\section*{Solutions}

\begin{enumerate}

    \item In order to make the code reusable, we choose to group all the
    interfaces for the classes in a header file, while the implementations are
    in a corresponding source file. There is also a separate header file for
    the common definitions, such as \cpp{enum}s and \cpp{typedef}s

    \lstset{basicstyle=\scriptsize\sf}
    \lstinputlisting{./src/v1/src/rootfinding.hpp}
    \lstset{basicstyle=\sf}

    In this case, the name defined by \cpp{rootfinding.hpp} are exported in the
    \cpp{Rootfinding} \cpp{namespace}.

    here follow the listings for the header file and source file that implement
    the \cpp{Bisection} and \cpp{Newton} classes.
    % Bisection
    \lstset{basicstyle=\scriptsize\sf}
    \lstinputlisting[caption=\cpp{Bisection} class interface]
      {src/v1/src/bisection.hpp}
    \lstset{basicstyle=\sf}
    note that all the private attributes in the class are in the form
    \cpp{M_attributename}. This is a common approach to clearly state in the
    implementations which variables are local and which are class variables.

    The \cpp{converged} method is an \cpp{inline} function. The \cpp{inline}
    directive tells the compiler that it should try to insert the whole body of
    the function where it is called, instead of issueing a call. As it is done
    in the code, it is still possible to separate the declaration and the
    definition of the method. All the methods that are directly declared inside
    a class are treated as \cpp{inline}. If the method is instead implemented
    outside the class, it must be in a header file. It is common practice to
    insert only very short methods directly inside the class. The \cpp{inline}
    directive is available also for free functions, and it behaves in the same
    way.
    \lstset{basicstyle=\scriptsize\sf}
    \lstinputlisting[caption=\cpp{Bisection} class implementation]
    {src/v1/src/bisection.cpp}
    \lstset{basicstyle=\sf}
    Inside the constructor of the class it is possible to call explicitly the
    constructors of the attributes of the class. In this case, the members 
    \cpp{M_tol}, \cpp{M_maxit} and \cpp{M_check} are initialized directly from
    the values of \cpp{tol}, \cpp{maxit} and \cpp{check}, respectively.

    The notes for the \cpp{Newton} class are analogous to the ones for the
    \cpp{Bisection} class. Here follows the listings for this class
    % Newton
    \lstset{basicstyle=\scriptsize\sf}
    \lstinputlisting[caption=\cpp{Newton} class interface]
    {src/v1/src/newton.hpp}
    \lstinputlisting[caption=\cpp{Newton} class implementation]
    {src/v1/src/newton.cpp}
    \lstset{basicstyle=\sf}


    \item Here follows the listings for the \cpp{Robust} class, subdivided in a
    header file and a source file.
    % Robust
    \lstset{basicstyle=\scriptsize\sf}
    \lstinputlisting[caption=\cpp{Robust} class interface]
        {src/v1/src/robust.hpp}
    \lstinputlisting[caption=\cpp{Robust} class implementation]
        {src/v1/src/robust.cpp}
    \lstset{basicstyle=\sf}

    inside the \cpp{Robust} class there are the definitions for the types for
    the coarse approximation of the zero (\cpp{coarseT}) and for the high order
    method used to refine the result (\cpp{fineT}). This can be useful if we
    decide to change one of the two methods with another one that uses the same
    interface. The student can try to implement as an homework a modified
    Newton method and introduce it in \cpp{Robust}. In the future we will see
    how to generalize this technique with the introduction of templates.
    The tolerance required for the coarse method is defined as the product of
    the final tolerance times the \cpp{M\_cfratio} (\emph{coarse-to-fine ratio})
    coefficient. The accessor member functions allow the access to
    non-modifiable memebers with a \cpp{const} method.

    The \cpp{Makefile} that compiles \cpp{bisection}, \cpp{newton} and
    \cpp{robust} produces a static library named \cpp{librootfinding.a}. Once
    the library is available, it can be used by other programs, such as the ones
    for \emph{Point 3}, specifying to the compiler the place (with
    \texttt{-Llib}) and the library name (with \texttt{-lrootfinding}).

    The structure for the \cpp{bisection}, \cpp{newton} and \cpp{robust} has a
    lot of common features. Replicating similar definitions is not only poor
    design, but can also lead to errors, for example if the classes must have a
    consistent interface. A better approach would be to create
    \cpp{IterativeMethod} base class from which all the other three derive.

    \item Here follows the code for the \emph{main program} for the solution of
    the third point. The implementation that it refers to is the one introduced
    in the previous points.
    \lstset{basicstyle=\scriptsize\sf}
    \lstinputlisting{./src/v1/bn.hpp}
    \lstset{basicstyle=\sf}
    in this case the \cpp{Bisection}, \cpp{Newton} and \cpp{Robust} classes are
    stored in the \verb!./src/! folder.
    \lstset{basicstyle=\scriptsize\sf}
    \lstinputlisting{./src/v1/bn.cpp}
    \lstset{basicstyle=\sf}

    Note in the \verb1main1 the use of two namespaces, \cpp{std} and
    \cpp{RootFinding}.

    \item Here follows the implementation for the \cpp{Robust} class. The
    overloading of \cpp{operator<<} is totally similar for the other two
    classes.
    \lstset{basicstyle=\scriptsize\sf}
    \lstinputlisting[linerange={15-29}]{./src/v2/src/robust.cpp}
    \lstset{basicstyle=\sf}

    Note that the function that overloads the stream operator uses the private
    members of the class. This is the reason why it is declared as a
    \cpp{friend} function with the definition
    \lstset{basicstyle=\scriptsize\sf}
    \lstinputlisting[linerange=46-47]{./src/v1.5/src/robust.hpp}
    \lstset{basicstyle=\sf}

    The code can be tested with the following \emph{main program}
    %
    \lstset{basicstyle=\scriptsize\sf}
    \lstinputlisting{./src/v1.5/bn.cpp}
    \lstset{basicstyle=\sf}
    %
    and this is the corresponding \emph{output}
    \begin{verbatim}
0.707107
* Robust Method *
Tolerance           :   1e-06
Max # of iterations :   100
Convergence check   :   increment
# of iterations (C) :   3
# of iterations (F) :   4
C-to-F tol ratio    :   200000
    \end{verbatim}

\end{enumerate}
